package suanfa;

/**
 * Definition for singly-linked list.
 * public class ListNode {
 * int val;
 * ListNode next;
 * ListNode(int x) { val = x; }
 * }
 */
public class test3 {

    public class ListNode {
        int val;
        ListNode next;

        ListNode(int x) {
            val = x;
        }
    }

    public ListNode removeNthFromEnd(ListNode head, int n) {

        int count=1;
        ListNode pre=new ListNode(0);
        pre=head;
       while (head.next!=null){
           count++;
           head=head.next;
       }
       head=pre;
        for (int i = 0; i < count-n-1; i++) {
            head=head.next;
        }
        if(count==n)
            return pre.next;
        if(n==1&&count==1)
        return null;
        if(n==1&&count!=1)
           head.next=null;
        return pre;
    }
   /* public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode cur = new ListNode(0);
        ListNode pre = cur;
        int carry = 0, sum = 0;
        while (l1 != null || l2 != null) {
            int num1 = l1 == null ? 0 : l1.val;
            int num2 = l2 == null ? 0 : l2.val;
            sum = num1 + num2;
            carry = sum / 10;
            sum = sum % 10;
            ListNode node = new ListNode(sum);
            cur.next = node;
            cur=node;
            if (l1 != null)
                l1 = l1.next;
            if (l2 != null)
                l2 = l2.next;
        }
        if (carry == 1) {
            ListNode node = new ListNode(1);
            cur.next = node;
        }
        return pre.next;
    }*/
}
